'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	const collection = db.collection('user-info')
	if (event.type == 'get') {
		let result = await collection.where({
			openid: event.openid
		}).get()
		if (result.affectedDocs) return result.data[0]
	}
	if (event.type == 'edit') {
		let result = await collection.where({
			openid: event.openid
		}).get()
		if (result.affectedDocs !== 0) {
			collection.where({
				openid: event.openid
			}).update({
				nickName: event.nickName,
				avatarUrl: event.avatarUrl
			})

		} else {
			let res = await collection.add(event)
		}
		return event
	}
};
